package com.seo.controller.user;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.seo.function.PageTool;
import com.seo.pojo.ReturnBody;
import com.seo.service.sysUser.sdk.SysUserService;
import com.seo.service.user.role.sdk.UserRoleService;
import com.seo.service.user.sdk.ConfigService;
import com.seo.service.user.sdk.UserService;
import com.seo.utils.ClientConst;
import com.seo.utils.Const;
import com.seo.utils.DateUtil;
import com.seo.utils.Jurisdiction;

import lombok.extern.slf4j.Slf4j;

/**
 * 用户相关controller
 * 
 * @author Administrator
 * @version 2018.08.02 17:40
 */
@Slf4j
@Controller
@RequestMapping(value = "/user")
public class UserController {

	@Autowired
	private UserService userService;

	@Autowired
	private PageTool pageTool;

	@Autowired
	private UserRoleService userRoleService;

	@Autowired
	private SysUserService sysUserService;

	@Autowired
	private ConfigService configService;

	@RequestMapping
	public String list(@RequestParam Map<String, Object> params, HttpServletRequest request, HttpSession session) {
		log.info("进入用户列表...");
		String yTime = DateUtil.getAfterDayDate(DateUtil.getTimesmorning(), -1, 1);
		params.put("yTime", yTime);
		Integer id = Jurisdiction.getT("id", Integer.class, ClientConst.SESSION_USER);
		Map<String, Object> sysUser = sysUserService.getSysUserById(id);
		String dataAuthority = (String) sysUser.get("dataAuthority");
		if ("00".equals(dataAuthority)) {
			params.put("belongId", id);
		}

		PageInfo<?> page = pageTool.getZdyPage(userService::getUserCnt, userService::getUserList, params);
		request.setAttribute("page", page);
		String userType = Jurisdiction.getT("userType", String.class);
		request.setAttribute("userType", userType);
		if ("01".equals(userType)) {
			Map<String, Object> p = new HashMap<>();
			List<?> sysUserList = pageTool.getListPage(sysUserService::getSysUserList, p, 1, 100);
			request.setAttribute("sysUserList", sysUserList);
		}
		return "user/list";

	}

	@RequestMapping("/childList")
	public String childList(@RequestParam Map<String, Object> params, HttpServletRequest request, HttpSession session) {
		log.info("进入用户列表...");
		String yTime = DateUtil.getAfterDayDate(DateUtil.getTimesmorning(), -1, 1);
		params.put("yTime", yTime);

		PageInfo<?> page = pageTool.getZdyPage(userService::getUserCnt, userService::getUserList, params);
		request.setAttribute("page", page);
		String userType = Jurisdiction.getT("userType", String.class);
		request.setAttribute("userType", userType);
		return "user/childList";

	}

	@RequestMapping("/add")
	public String add(HttpServletRequest request) {
		log.info("进入添加用户页面");
		List<String> levelList = null;
		List<Map<String, Object>> roleList = userRoleService.getAllRoleList();
		String clevel = configService.getConfig("vip_level");
		if (clevel != null) {
			String[] level = clevel.split(",");
			levelList = Arrays.asList(level);
		}
		request.setAttribute("levelList", levelList);
		request.setAttribute("roleList", roleList);
		request.setAttribute("action", "addUser");
		return "user/edit";
	}

	@RequestMapping("/addUser")
	public String addRole(@RequestParam Map<String, Object> params, HttpServletRequest request) {
		log.info("保存用户");
		ReturnBody rb = ReturnBody.init();
		rb = userService.addUser(params);
		request.setAttribute("result", rb.getResult());
		return "save_result";
	}

	@RequestMapping("/edit")
	public String edit(Integer userId, HttpServletRequest request) {
		log.info("进入编辑用户页面");
		List<String> levelList = null;
		List<Map<String, Object>> roleList = userRoleService.getAllRoleList();
		Map<String, Object> user = userService.getUserById(userId);
		String clevel = configService.getConfig("vip_level");
		if (clevel != null) {
			String[] level = clevel.split(",");
			levelList = Arrays.asList(level);
		}
		request.setAttribute("levelList", levelList);
		request.setAttribute("roleList", roleList);
		request.setAttribute("user", user);
		request.setAttribute("action", "editUser");
		return "user/edit";
	}

	@RequestMapping("/toResetPwd")
	public String toResetPwd(Integer userId, HttpServletRequest request) {
		log.info("进入用户重置密码页面");
		request.setAttribute("userId", userId);

		return "user/resetPwd";
	}

	@RequestMapping("/resetPwd")
	@ResponseBody
	public ReturnBody resetPwd(Integer id, String password, HttpSession session) {
		log.info("更改用户密码...");
		ReturnBody rb = ReturnBody.init();
		if (id == null || id <= 0) {
			return rb.error("error", "参数不能为空");
		}

		Map<String, Object> params = new HashMap<>();
		params.put("id", id);
		params.put("password", password);

		return userService.updateResetUserPwd(params);
	}

	@RequestMapping("/editUser")
	public String editUser(@RequestParam Map<String, Object> params, HttpServletRequest request) {
		log.info("保存用户");
		ReturnBody rb = ReturnBody.init();
		rb = userService.updateUser(params);
		request.setAttribute("result", rb.getResult());
		return "save_result";
	}

	@RequestMapping("/deleteUser")
	@ResponseBody
	public ReturnBody deleteUser(Integer id, HttpSession session) {
		log.info("删除用户...");
		ReturnBody rb = ReturnBody.init();
		if (id == null || id <= 0) {
			return rb.error("error", "参数不能为空");
		}

		Map<String, Object> user = (Map<String, Object>) session.getAttribute(Const.SESSION_USER);
		Integer userId = (Integer) user.get("id");
		if (userId == id) {
			return rb.error("error", "不能删除自己");
		}

		return userService.deleteUserFalse(id, "01");
	}

	@RequestMapping("/updateState")
	@ResponseBody
	public ReturnBody updateState(Integer id, String state, HttpSession session) {
		log.info("更改用户状态...");
		ReturnBody rb = ReturnBody.init();
		if (id == null || id <= 0) {
			return rb.error("error", "参数不能为空");
		}

		if (!(StringUtils.equals(state, "00") || StringUtils.equals(state, "01"))) {
			return rb.error("error", "状态错误");
		}

		return userService.updateUserState(id, state);
	}

	@RequestMapping("/updateBelongName")
	@ResponseBody
	public ReturnBody updateBelongName(Integer userId, String belongName, HttpSession session) {
		log.info("更改用户归属...");
		ReturnBody rb = ReturnBody.init();
		if (userId == null || userId <= 0) {
			return rb.error("error", "请选择用户");
		}

		if (StringUtils.isEmpty(belongName)) {
			return rb.error("error", "请输入归属用户");
		}

		return sysUserService.updateBelongName(userId, belongName);
	}
}
